package com.apexsoft.assets.base.utils;


import com.apexsoft.assets.base.model.AuthUser;
import com.apexsoft.assets.base.model.User;
import com.apexsoft.live.session.UserAuthenticateSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/3/13.
 */
@Component
public class UserSession extends UserAuthenticateSession {
    private static final Logger LOG = LoggerFactory.getLogger(UserSession.class);

    public static HttpServletRequest getRequestHolder() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    public static HttpServletResponse getResponseHolder() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getResponse();
    }

    public static String getAccessToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getSessionId();
    }

    public static AuthUser<User> getUserSession() {
        return getUser(getRequestHolder());
    }

    public static void setUserSession(Object user) {
        setUser(getRequestHolder(), user);
    }

    public static void removeUserSession() {
        removeUser(getRequestHolder());
    }

}
